// pages/my/my.js
const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{
      company_name:"未登录"
    },
    root:2,
    // worktime:"100",
    hasUserInfo: false,
    userInfo: {},
    phoneNumber: '',
    working_hours:"100",
    listone:[],
    listtwo:[],
    listthree:[],
    // workname:"经理",
    roleslist:["","超级管理员","管理员","生产负责人","质检员","操作工"],
    lsit:[],
    workname:"",
    checked:false,//消息推送
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"options")
    var that = this;
    let tmp = {
      account:"admin",
      password:"123456"
    }
      request.requestAjax('login',tmp,'加载中',function(res){
        console.log(res,"AAAAAAAAAAAAA")

          that.setData({
            info:res.data,
            listone:res.data,
            // worktime:res.
          });
        console.log(that.data.userInfo,"1111111111")

      },function(res){
          wx.showToast({
            title: '加载失败',
            icon:'error'
          });
      },true)
      this.checkAuthStatus()
      request.requestAjax('userinfo', {}, "加载中", function(res) {
        if (res.status == 200) {
            that.setData({
              listthree:res.data,
              workname:that.data.roleslist[res.data.role]
            });
            console.log(that.data.listthree,"listthree")
            // return false;
        } else {
            wx.showToast({
                title: '请求失败',
                icon: "none"
            })
        }
  
      }, function(res) {
          wx.showToast({
              title: '请求失败',
              icon: "none"
          })
      });
  },

  
  logout(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定退出当前账号吗？',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          that.logoutHandle();
        } 
      }
    })
  },
  logoutHandle(){
    wx.removeStorageSync('token')
      wx.removeStorageSync('expires_time')
      wx.showToast({
        title: '退出成功',
        icon:"success"
      })
      setTimeout(function(){
        wx.navigateTo({
          url: '/pages/login/login',
        })
      },1000);
      return false;
    request.requestAjax('logout',{},'安全退出中',function(res){
      wx.removeStorageSync('token')
      wx.removeStorageSync('expires_time')
      wx.showToast({
        title: '退出成功',
        icon:"success"
      })
      setTimeout(function(){
        wx.navigateTo({
          url: '/pages/login/login',
        })
      },1000);

  },function(res){
      wx.showToast({
        title: '退出失败',
        icon:'error'
      });
  },true)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("onshow")
    this.onLoad({})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  setroot(e){
    console.log(e,"eeeeeeeeeee")
  },
  checkAuthStatus() {
    var that = this
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          // 已授权，直接获取用户信息
          wx.getUserInfo({
            success: (res) => {
              this.setData({
                hasUserInfo: true,
                userInfo: res.userInfo
              });
              console.log(that.data,"AAAAAASSSSSSSDDDDDDDDDDD")
            }
          });
        }
      },
      fail:(res)=>{
          console.log(res,"泰罗")

          console.log(that.data,"1111111")
          console.log(that.data.userInfo.hasUserInfo,"1111111")

      }
    });
    console.log(this.data,"aaaaaaaaaaaaaa")
  },

  handleGetUserInfo(e) {
    console.log(e,"eeeeeeee")
    if (e.detail.userInfo) {
      // 用户允许授权
      this.setData({
        hasUserInfo: true,
        userInfo: e.detail.userInfo
      });
      // 可将信息发送到后端存储
      this.sendUserInfoToServer(e.detail.userInfo);
    } else {
      // 用户拒绝授权
      wx.showToast({
        title: '授权失败，部分功能受限',
        icon: 'none'
      });
    }
  },


  working_hour_record(e){
    wx.navigateTo({ url: '/pages/working_hour_record/working_hour_record' })
  },
  onSwitchChange(e){
    console.log(e,"eeeeeeeeeee")
      // this.setData({checked:!this.data.checked})
      if(e.detail=== true){
        var that = this
        wx.getSetting({
          success: (res) => {
            if (res.authSetting['scope.userInfo']) {
              // 已授权，直接获取用户信息
              wx.getUserInfo({
                success: (res) => {
                  this.setData({
                    hasUserInfo: true,
                    userInfo: res.userInfo
                  });
                  console.log(that.data,"AAAAAASSSSSSSDDDDDDDDDDD")
                }
              });
            }
          },
          fail:(res)=>{
              wx.showModal({
                title: '未授权',
                content: '请授权当前账号',
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后
                    // that.logoutHandle();
                    // wx.showModal({
                    //   // title: '未授权',
                    //   title: "用户授权",
                    // })
                    wx.showToast({
                      title: '授权成功',
                      icon: 'success', // 可选值：success/loading/none
                      duration: 1000   // 默认1500ms
                    });
                    that.setData({ checked: e.detail });
                  } 
                }
              })
    
          }
        });
      }else{
        //用户取消接收通知
        this.setData({ checked: e.detail });
        wx.showToast({
          title: '用户取消授权',
          icon: 'success', // 可选值：success/loading/none
          duration: 1000   // 默认1500ms
        });
        this.setData({ checked: e.detail });
      }
      // this.setData({ checked: e.detail });
    // if(this.data.checked === true){
    //   this.setData({add_ticket:1})
    // }
  }
})